﻿
$(document).ready(function() {
	$.cookie("web_num", "", {
		path : '/',
		expires: -1
	});
	
		showCustomerDetail();
//		logout();
	$("#reset-password").submit(function() {
			var data = $(this).serialize();
			$.ajax({
					url : "/account/findpwd.json",
					data : data,
					type : "post",
					async : false,
					context : document.body
				}).success( function(data) {
						if (data.code == 1) {
							var alert = '<div class="alert alert-success alert-dismissable">'
									+ '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'
									+ '<strong>邮件已发送！</strong> 请登陆您的邮箱按邮件提示操作！'
									+ '</div>';
							var alertDIV = $("#reset-password").find("div.alert-info");
							$("#reset-password").find(".alert").alert('close');
							alertDIV.append(alert);
						} else {
							var alert = '<div class="alert alert-danger alert-dismissable">'
									+ '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'
									+ '<strong>错误！</strong>'
									+ data.message
									+ '</div>';
							var alertDIV = $("#reset-password").find("div.alert-info");
							$("#reset-password").find(".alert").alert('close');
							alertDIV.append(alert);
						}
							});
			return false;
		});
	// 登录
	$("#login-form").submit(function() {
			var url = purl();
			$('#app-name').val("admin");
			var data = $(this).serialize();
			var ref = url.param("ref");
			$.ajax({
					url : "/server/session/login.json",
					data : data,
					type : "post",
					async : false
				}).success(function(rtn) {
				if(rtn.code=="5"){
					cleanUserCookie();
					
					alert("当前用户在不常用浏览器登陆 请重新输入密码");
					
				}
				if (rtn.code != 1) {
					var alertInfo = '<div class="alert alert-danger alert-dismissable">'
							+ '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'
							+ '<strong>登录失败！</strong> '
							+ rtn.message
							+ '</div>';
					var alertDIV = $("#login-form").find("section.alert-info");
					$("#login-form").find(".alert").alert('close');
					alertDIV.append(alertInfo);
				} else {
					saveCookie();
					saveLoginCount();
//					synchronizeToken();
					if (ref)
						location.href = ref;
					else
						window.location.href = '../../../index/index/page/index.html';
				}

				});
			return false;
		});
	
	//自动登录操作
//	autoLogin();
	autoPwd();

});

//function logout() {
//	$.ajax({
//		url : "/server/session/logout.json",
//		type : "post",
//		async : false
//	});
//	$.cookie('lmspd_user', null, {
//		path : '/'
//	});
//	
//	cleanUserCookie();//清除自动登录
//}

// 保存本次登录信息
function saveLoginCount() {
	var url = "/server/session/saveLoginCount.json";
	$.getJSON(url, function(rtn) {
		console.log(rtn.code);
		
	})
}

/**
function synchronizeToken(){
	var url = "/server/session/synchronizeToken.json";
	$.post(url, function(rtn){});
}*/

// 登录页面自动初始化客户信息
function showCustomerDetail() {
	var url = "/server/customer/info/detail.json";
	var data = {
		'id' : "0FA1C730-9552-46A7-A632-7784224414CE",
	}
	$.getJSON(url, data, function(rtn) {
		$("#extr").children("li:eq(2)").find("a").attr("href", rtn.url);
		$('#customer-customerCode').val(rtn.customerCode);
		$("#welcome").html(rtn.welcomeMessage);
		$("#bottom").html(rtn.copyRight);
		$("#content img").attr('src', rtn.logo);
		$('title').html(rtn.title);
	});

}


function saveCookie() {
	if ($("#ck-rmbUser:checked").length == 1) {
		var loginid = $("#loginid").val();
		var password = $("#password").val();
		$.cookie("rmbUser", "true", {
			path : '/',
		    expires: 30
		});

		// 存储一个带7天期限的cookie
		$.cookie("loginid", loginid, {
			path : '/',
			expires: 30
		});
		$.cookie("lmsp", $.md5(password), {
			path : '/',
			expires: 30
		});
		
		
	} else {
		$.cookie("lmspd_user", $.cookie("lmspd_user"), {
			path : '/',
		});
		
		$.cookie("web_num", $.cookie("web_num"), {
			path : '/',
		});
		
	}
}

//自动添加用户名密码  
function autoPwd() {
	if ($.cookie("rmbUser") == "true") {
			$("#loginid").val($.cookie("loginid"));
			$("#password").val($.cookie("lmsp"));
			$("#automaticlogin").val(true)
			$("#lastLoginToken").val($.cookie("lmspd_user"))
			$(".btn-primary").trigger("click");
			 
	}
}
 

//清除自动登录和记住密码
function cleanUserCookie(){
	 
	$.cookie("rmbUser", "false", {
		path : '/',
		expires: -1
	});
	$.cookie("loginid", "", {
		path : '/',
		expires: -1
	});
	$.cookie("lmsp", "", {
		path : '/',
		expires: -1
	});
	
	$.cookie("automaticlogin", false, {
		path : '/',
		expires: -1
	});
	
	$("#loginid").val($.cookie("loginid"));
	$("#password").val($.cookie("lmsp"));
	$("#automaticlogin").val(false);
}
